<template>
  <view class="page-login">
    <!-- <view class="back">
      <uni-icons type="left" color="#ffffff" size="24" @click="handleBack"></uni-icons>
    </view> -->
    <view class="page-content">
      <view class="info">
        <img src="~@/static/images/login/login-icon.png" alt="" srcset="" class="info-logo" />
        <text class="info-title">{{ $t('login.AB') }}</text>
      </view>
      <view class="form">
        <view class="login-form">
          <view class="form-txt">{{ $t('login.登录') }}</view>
          <view class="input-item flex align-center">
            <img src="~@/static/images/login/username.webp" alt="" srcset="" class="input-icon" />
            <input v-model="loginForm.username" class="input" type="text" :placeholder="$t('login.请输入账号')" maxlength="30" />
          </view>
          <view class="input-item flex align-center">
            <img src="~@/static/images/login/password.webp" alt="" srcset="" class="input-icon" />
            <uni-easyinput v-model="loginForm.password" type="password" class="input" :placeholder="$t('login.请输入密码')" maxlength="20" />
          </view>
          <view class="action-btn">
            <button @click="handleLogin" class="login-btn">{{ $t('login.登录') }}</button>
          </view>
          <view class="form-options">
            <view class="options-item" @click="handleUserRegister">{{ $t('login.注册') }}</view>
            <view class="options-item" @click=handleForgetPwd>{{ $t('login.忘记密码') }}？</view>
          </view>
        </view>
      </view>
      <!-- 语言 -->
      <view class="lang-value" @click="handleChangeLang">{{ lang }}</view>
    </view>
    <uni-popup ref="popup" type="bottom" border-radius="10px 10px 0 0">
      <view class="popup-wrapper">
        <view class="popup-header">
          <view>{{ $t('mine.设定') }}</view>
          <uni-icons type="closeempty" color="#dddddd" size="24" class="close-icon" @click="handleClose"></uni-icons>
        </view>
        <view class="popup-content">
          <view class="popup-item" v-for="item in messagesList" :key="item.value" @click="handleSelectLang(item.value)">{{ item.label }}</view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
  import { setLang, getLang } from '@/utils/auth'
  import { messages } from '@/lang/index.js'

  export default {
    data() {
      return {
        messagesList: [],
        globalConfig: getApp().globalData.config,
        loginForm: {
          username: '',
          password: ''
        }
      }
    },
    methods: {
      //返回
      handleBack() {
        this.$tab.navigateBack()
      },
      // 初始化多语言
      initLang() {
        this.messagesList = Object.keys(messages).map((key) => {
          return {
            label: messages[key].language,
            value: key
          }
        })
        let lang = getLang() || 'Korean'
        let langLabel = this.messagesList.find((item) => item.value === lang)?.label
        this.lang = langLabel
      },
      // 切换语言
      handleChangeLang() {
        this.$refs.popup.open('bottom')
      },
      // 选择语言
      handleSelectLang(lang) {
        this.flag = true
        this.lang = this.messagesList.find((item) => item.value === lang)?.label
        this.$refs.popup.close()
        setLang(lang)
        this.$i18n.locale = lang
      },
      //关闭
      handleClose() {
        this.$refs.popup.close()
      },
      // 用户注册
      handleUserRegister() {
        this.$tab.redirectTo(`/pages/register`)
      },
      //忘记密码
      handleForgetPwd(){
         this.$tab.redirectTo(`/pages/forgetPwd`)
      },
      // 登录方法
      async handleLogin() {
        if (this.loginForm.username === '') {
          this.$modal.msgError(this.$t('login.请输入账号'))
        } else if (this.loginForm.password === '') {
          this.$modal.msgError(this.$t('login.请输入密码'))
        } else {
          this.$modal.loading(this.$t('login.登录中，请耐心等待'))
          this.pwdLogin()
        }
      },
      // 密码登录
      async pwdLogin() {
        this.$store
          .dispatch('Login', this.loginForm)
          .then(() => {
            this.$modal.closeLoading()
            this.loginSuccess()
          })
          .catch(() => {
            this.$modal.closeLoading()
          })
      },
      // 登录成功后，处理函数
      loginSuccess(result) {
        // 设置用户信息
        this.$store.dispatch('GetInfo').then((res) => {
          this.$tab.reLaunch('/pages/index')
        })
      }
    },
    created() {
      this.initLang()
    }
  }
</script>

<style lang="scss" scoped>
  .page-login {
    width: 100%;
    height: 100vh;
    background: url(~@/static/images/login/login-bg-old.png) 0% 0% / 100% 100vh no-repeat;
    overflow: hidden;
    .page-content {
      height: 100%;
      overflow-y: auto;
    }
    .back {
      padding-left: 16rpx;
      padding-top: 16rpx;
    }
    .info {
      padding-top: 140rpx;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .info-logo {
        width: 160rpx;
        height: 160rpx;
      }
      .info-title {
        font-size: 60rpx;
        margin-top: 20rpx;
        margin-bottom: 50rpx;
      }
    }
    .form {
      width: 100vw;
      padding: 0 30rpx;
      margin-top: 20rpx;
      margin-bottom: 40rpx;
      .login-form {
        width: 100%;
        min-height: 50vh;
        border-radius: 10px;
        padding: 30px;
        box-sizing: border-box;
        background: hsla(0, 0%, 100%, 0.8);
        .form-txt {
          font-size: 55rpx;
          text-align: center;
        }
        .input-item {
          margin: 20px auto;
          height: 45px;
          border-bottom: 1rpx solid #6083d6;
          display: flex;
          align-items: center;
          .input-icon {
            width: 44rpx;
            height: 44rpx;
          }
          .input {
            flex: 1;
            font-size: 14px;
            line-height: 20px;
            text-align: left;
            padding-left: 15px;
            ::v-deep.uni-easyinput__content {
              border: none !important;
              background-color: transparent !important;
            }
          }
        }

        .login-btn {
          width: 70vw;
          // background: linear-gradient(90deg, #6284d6, #2d63e3);
          background: linear-gradient(90deg, #31d6e0, #5599e0);
          margin: 50rpx auto 40rpx;
          height: 80rpx;
          border-radius: 20px;
          font-size: 32rpx;
          color: #fff;
          line-height: 80rpx;
          text-align: center;
        }
        .login-code {
          height: 38px;
          float: right;
          .login-code-img {
            height: 38px;
            // position: absolute;
            margin-left: 10px;
            width: 200rpx;
          }
        }
        .form-options {
          font-size: 32rpx;
          display: flex;
          justify-content: space-between;
          align-items: center;
          color: #3d3d3d;
        }
      }
    }
    .lang-value {
      position: fixed;
      top: 40px;
      right: 16px;
      z-index: 9;
      border: 1px solid #5a7c7d;
      padding: 5px 10px;
      border-radius: 6px;
      color: #5a7c7d;
    }
    .popup-wrapper {
      background-color: #fff;
      .popup-header {
        padding: 12px 30px;
        text-align: center;
        position: relative;
        .close-icon {
          position: absolute;
          right: 10px;
          top: 50%;
          transform: translateY(-50%);
        }
      }
      .popup-content {
        .popup-item {
          padding: 15px;
          display: flex;
          justify-content: center;
          align-items: center;
          border-bottom: 1px solid #d6d7d9;
        }
      }
    }
  }
</style>
